package com.dingreading.cloud.admin.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dingreading.cloud.admin.entity.AdminButton;
import com.dingreading.cloud.admin.entity.AdminRoleButton;
import com.dingreading.cloud.admin.service.AdminButtonService;
import com.dingreading.cloud.admin.service.AdminRoleButtonService;
import com.dingreading.cloud.common.base.R;
import com.dingreading.cloud.common.util.DescribeException;
import com.dingreading.cloud.common.util.JsonUtil;
import com.dingreading.cloud.common.util.PageBeanDto;
import com.dingreading.cloud.common.util.PageUtil;
import com.mybatisflex.core.paginate.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/system/adminRoleButton")
@Api(tags = "总部-总部角色关联总部按钮管理")
public class AdminRoleButtonController {

    @Resource
    private AdminButtonService buttonService;
    @Resource
    private AdminRoleButtonService adminRoleButtonService;


    @ApiOperation(value = "根据角色获取按钮列表")
    @PostMapping(value = "/pageList", produces = "application/json; charset=UTF-8")
    public R<Object> pageList(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "分页公共类") PageUtil pageUtil,
            @ApiParam(value = "角色id") @RequestParam(value = "roleId") Long roleId
    ) {
        Page<AdminRoleButton> page = adminRoleButtonService.pageList(pageUtil, roleId);

        JSONArray array = new JSONArray();
        List<AdminRoleButton> list = page.getRecords();
        if (CollectionUtils.isNotEmpty(list)) {
            List<Long> ids = list.stream().map(AdminRoleButton::getButtonId).collect(Collectors.toList());
            List<AdminButton> buttonList = buttonService.getByIds(ids);
            Map<Long, AdminButton> map = new HashMap<>();
            if (CollectionUtils.isNotEmpty(buttonList))
                map = buttonList.stream().collect(Collectors.toMap(AdminButton::getId, Function.identity()));

            for (AdminRoleButton adminRoleButton : list) {
                JSONObject jsonObject = JsonUtil.toJsonObject(adminRoleButton);
                AdminButton button = map.get(adminRoleButton.getButtonId());
                jsonObject.put("name", button != null ? button.getName() : "");
                jsonObject.put("remark", button != null ? button.getRemark() : "");
                array.add(jsonObject);
            }
        }

        PageBeanDto dto = new PageBeanDto();
        dto.setTotal(page.getTotalRow());
        dto.setList(array);
        return R.ok(dto);
    }


    @Transactional(rollbackFor = DescribeException.class)
    @ApiOperation(value = "添加按钮到角色")
    @PostMapping(value = "/newBean", produces = "application/json; charset=UTF-8")
    public R<Object> newBean(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "角色id") @RequestParam(value = "roleId") Long roleId,
            @ApiParam(value = "按钮id") @RequestParam(value = "id") Long id
    ) {
        long data = adminRoleButtonService.getByButtonId(id);
        if (data > 0)
            return R.fail("不能重复加入");

        AdminRoleButton AdminRoleButton = new AdminRoleButton();
        AdminRoleButton.setRoleId(roleId);
        AdminRoleButton.setButtonId(id);
        adminRoleButtonService.saveOrUpdate(AdminRoleButton);

        return R.ok();
    }


    @ApiOperation(value = "删除角色的按钮权限")
    @DeleteMapping(value = "/dropBean", produces = "application/json; charset=UTF-8")
    public R<Object> dropBean(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "角色按钮id") @RequestParam(value = "id") Long id
    ) {
        int l = adminRoleButtonService.dropBean(id);
        if (l <= 0) return R.fail("删除失败");

        return R.ok();
    }

}
